package com.zxx.mvpPractice.presenter;


import android.support.annotation.NonNull;
import android.util.Log;

import com.google.common.base.Preconditions;
import com.zxx.mvpPractice.base.RxPresenter;
import com.zxx.mvpPractice.model.net.RetrofitHelper;
import com.zxx.mvpPractice.model.net.RongyunApi;
import com.zxx.mvpPractice.model.net.bean.RongTokenRes;
import com.zxx.mvpPractice.presenter.contract.MainContract;
import com.zxx.mvpPractice.utils.KL;
import com.zxx.mvpPractice.utils.SystemUtils;

import java.util.HashMap;
import java.util.Map;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


/**
 * Created by Administrator on 2016/12/17.
 */
public class MainPresenter extends RxPresenter implements MainContract.Presenter {
    MainContract.View mView;

    public MainPresenter(@NonNull MainContract.View oneView) {
        mView = Preconditions.checkNotNull(oneView);
        mView.setPresenter(this);
    }

    @Override
    public void IM_Test() {
        Map<String, String> fieldMap = new HashMap<String, String>();
        Map<String, String> headerMap = new HashMap<String, String>();
        fieldMap.put("userId", "0");
        fieldMap.put("name", "zxx");
        fieldMap.put("portraitUri", "http://www.baidu.com");

        String Nonce = SystemUtils.getRandom();
        String Timestamp = SystemUtils.getTimes();
        String Signature = SystemUtils.sha1(RongyunApi.App_Secret + Nonce + Timestamp);

        headerMap.put("Nonce", Nonce);
        headerMap.put("Timestamp", Timestamp);
        headerMap.put("Signature", Signature);

        KL.d(MainPresenter.class, "Nonce:" + Nonce + ",Timestamp:" + Timestamp + ",Signature:" + Signature);

        RetrofitHelper.getRongyunApi().getToken(headerMap, fieldMap)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<RongTokenRes>() {
                    @Override
                    public void onStart() {  //先显示对话框
                        mView.showProgressDialog();
                    }

                    @Override
                    public void onCompleted() {  //请求结束，对话框消失
                        mView.hideProgressDialog();
                    }

                    @Override
                    public void onError(Throwable e) {   //error时
                        Log.d("zxx", "showErrorMessage:" + e.getMessage());
                        mView.showMessage("token失败");
                        mView.hideProgressDialog();
                    }

                    @Override
                    public void onNext(RongTokenRes msg) {
//                        mView.showBmob(msg);
                        if (msg.getCode() == 200) {
                            mView.showMessage("token成功");
                            Log.d("zxx", "onNext:" + msg.toString());

                        } else {
                            mView.showMessage("token失败");
                            Log.d("zxx", "onNext:" + msg.toString());
                        }
                        mView.hideProgressDialog();
                    }
                });
    }
}
